package com.huicoo.forestmanager.ui.login.presenter;


import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.huicoo.forestmanager.base.BaseApplication;
import com.huicoo.forestmanager.network.bean.login.LoginData;
import com.huicoo.forestmanager.network.bean.login.LoginResData;
import com.huicoo.forestmanager.others.Constants;
import com.huicoo.forestmanager.ui.login.contract.LoginContract;
import com.huicoo.forestmanager.ui.login.model.LoginModel;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.SharedPreferenceUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginPresenter implements LoginContract.Presenter {
    private LoginContract.View view;
    private LoginContract.Model model;

    public LoginPresenter(LoginContract.View view) {
        this.view = view;
        this.model = new LoginModel();
    }

    @Override
    public void login(String account, String password) {
        Call<LoginResData> call = model.login(account, password);
        call.enqueue(new Callback<LoginResData>() {
            @Override
            public void onResponse(@NonNull Call<LoginResData> call, @NonNull Response<LoginResData> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo login result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.loginFail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        // 记录token等数据
                        LoginData data = response.body().getData();
                        String token = data.getToken();
                        String userId = data.getUserId();
                        String userRealName = data.getUserRealName();
                        String userLevel = data.getUserLevel();
                        String userType = data.getUserType();
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.TOKEN, token);
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.USER_ID, userId);
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.USER_REAL_NAME, userRealName);
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.USER_ACCOUNT, account);
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.USER_PASSWORD, password);
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.USER_LEVEL, userLevel);
                        SharedPreferenceUtil.putData(Constants.PROJECT_NAME, Constants.USER_TYPE, userType);


                        CloudPushService pushService = PushServiceFactory.getCloudPushService();
                        String s = SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.USER_ACCOUNT);
                        Log.d(BaseApplication.TAG, "获取设备ID: " + pushService.getDeviceId());
                        Log.d(BaseApplication.TAG, "当前账号 : " + s);
                        pushService.bindAccount(s, new CommonCallback() {
                            @Override
                            public void onSuccess(String s) {
                                Log.d(BaseApplication.TAG, "绑定账号成功");
                            }

                            @Override
                            public void onFailed(String s, String s1) {
                                Log.d(BaseApplication.TAG, "绑定账号失败");
                            }
                        });

                        if (view != null) {
                            view.loginSuccess();
                        }
                    } else {
                        if (view != null) {
                            view.loginFail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    if (view != null) {
                        view.loginFail("请求失败");
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<LoginResData> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.loginFail("请求失败");
                }
            }
        });

    }

    @Override
    public void destroy() {
        view = null;
    }
}
